package com.itheima.mall.web.pms;

import com.itheima.mall.pojo.pms.Product;
import com.itheima.mall.service.pms.ProductService;
import com.itheima.mall.vo.PageResult;
import com.itheima.mall.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    //创建商品
    @PostMapping("create")
    public R create(@RequestBody Product product){
        productService.create(product);
        return R.succeed(null,"添加成功");
    }

    //分页模糊查询查询商品
    @GetMapping("list")
    public R selectProduct(Integer brandId, String keyword,
                           Integer pageNum, Integer productCategoryId,
                           String productSn, Integer publishStatus,
                           Integer verifyStatus, Integer pageSize){
        PageResult pageResult = productService.list(brandId,keyword,pageNum,pageSize,productCategoryId,
                                                    productSn,publishStatus,verifyStatus);
        return R.succeed(pageResult,"添加成功");
    }

    /**
     * 批量修改删除状态
     * @param ids
     * @param deleteStatus
     * @return
     */
    @PostMapping("update/deleteStatus")
    public R updateDeleteStatus(Long[] ids, @RequestParam("deleteStatus") Integer deleteStatus){
        productService.updateStatus(ids,deleteStatus);
        return R.succeed(null,"修改成功");
    }

    /**
     * 批量设为新品
     * @param ids
     * @param newStatus
     * @return
     */
    @PostMapping("update/newStatus")
    public R updateNewStatus(Long[] ids,@RequestParam("newStatus") Integer newStatus){
        //复用updateDeleteStatus
        productService.updateStatus(ids,newStatus);
        return R.succeed(null,"修改成功");
    }

    /**
     * 批量设为新品
     * @param ids
     * @param publishStatus
     * @return
     */
    @PostMapping("update/publishStatus")
    public R publishStatus(Long[] ids,@RequestParam("publishStatus") Integer publishStatus){
        //复用updateDeleteStatus
        productService.updateStatus(ids,publishStatus);
        return R.succeed(null,"修改成功");
    }

    /**
     * 批量推荐商品
     * @param ids
     * @param recommendStatus
     * @return
     */
    @PostMapping("update/recommendStatus")
    public R recommendStatus(Long[] ids,@RequestParam("recommendStatus") Integer recommendStatus){
        //复用updateDeleteStatus
        productService.updateStatus(ids,recommendStatus);
        return R.succeed(null,"修改成功");
    }

    /**
     * 批量修改审核状态
     * @param ids
     * @param verifyStatus
     * @return
     */
    @PostMapping("update/verifyStatus")
    public R verifyStatus(Long[] ids,@RequestParam("verifyStatus") Integer verifyStatus,
                          @RequestParam("detail") String detail){
        //复用updateDeleteStatus
        productService.updateVerifyStatusStatus(ids,verifyStatus,detail);
        return R.succeed(null,"修改成功");
    }

    /**
     * 更新商品
     * @param id
     * @param product
     * @return
     */
    @PostMapping("update/{id}")
    public R updateById(@PathVariable("id") Long id,@RequestBody Product product){
        productService.updateById(id,product);
        return R.succeed(null,"修改成功");
    }


    /**
     * 根据商品id获取商品编辑信息
     * @param id
     * @return
     */
    @GetMapping("updateInfo/{id}")
    public R updateInfoById(@PathVariable("id") Long id){
        Product product = productService.updateInfoById(id);
        return R.succeed(product,"查询成功");
    }

    /**
     * 根据商品名称或货号模糊查询
     * @param keyword
     * @return
     */
    @GetMapping("simpleList")
    public R simpleList(String keyword){
        List<Product> productList = productService.simpleList(keyword);
        return R.succeed(productList,"查询成功");
    }
}
